using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Common.Utilities.DLR;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class RuleComponentInstruction : InstructionBase
    {
        #region props
        private string _EvaluableAssemblyFilePath;
        /// <summary></summary>
        public string EvaluableAssemblyFilePath
        {
            get
            {
                return this._EvaluableAssemblyFilePath;
            }
            set
            {
                this._EvaluableAssemblyFilePath = value;
            }
        }

        private string _EvaluableTypeName;
        /// <summary></summary>
        public string EvaluableTypeName
        {
            get
            {
                return this._EvaluableTypeName;
            }
            set
            {
                this._EvaluableTypeName = value;
            }
        }

        private Guid _RuleID;
        /// <summary></summary>
        public Guid RuleID
        {
            get
            {
                return this._RuleID;
            }
            set
            {
                this._RuleID = value;
            }
        }
        #endregion

        /// <summary>
        /// removed ORM
        /// </summary>
        public RuleComponentInstruction()
            : base(Guid.Empty, Guid.Empty)
        {
            this._RuleID = Guid.Empty;
            this._EvaluableTypeName = string.Empty;
            this._EvaluableAssemblyFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="ruleID"></param>
        /// <param name="assemFilePath"></param>
        /// <param name="typeName"></param>
        public RuleComponentInstruction(Guid componentID, Guid jobTicketID, 
            Guid ruleID, string assemFilePath, string typeName)
            : base(componentID, jobTicketID)
        {
            this._RuleID = ruleID;
            this._EvaluableAssemblyFilePath = assemFilePath;
            this._EvaluableTypeName = typeName;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RuleID", this.RuleID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EvaluableAssemblyFilePath", this.EvaluableAssemblyFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EvaluableTypeName", this.EvaluableTypeName);
            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                RuleComponentInstruction instruction = new RuleComponentInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.RuleID=new Guid(xNode.Attributes.GetNamedItem("RuleID").Value);
                instruction.EvaluableAssemblyFilePath = xNode.Attributes.GetNamedItem("EvaluableAssemblyFilePath").Value;
                instruction.EvaluableTypeName = xNode.Attributes.GetNamedItem("EvaluableTypeName").Value;

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
